//
//  CJSHotView.m
//  GoTraveling
//
//  Created by 陈金山 on 15/12/24.
//  Copyright © 2015年 开发华佗. All rights reserved.
//

#import "CJSHotView.h"
#import "CJSHotTableViewCell.h"
#import "MJRefresh.h"
#import "CJSStoryViewController.h"

@interface CJSHotView ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain) UITableView *tableView;
@property (nonatomic, assign) NSInteger start;

@end

@implementation CJSHotView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor colorWithRed:0.980 green:0.961 blue:0.910 alpha:1.000];
        [self createSubViews];
        self.start = 0;
    }
    return self;
}

- (void)createSubViews {
    
    self.tableView = [[UITableView alloc] initWithFrame:self.bounds style:UITableViewStylePlain];
    if ([[[NSUserDefaults standardUserDefaults] objectForKey:@"states"] isEqualToString:@"1"]) {
        self.tableView.backgroundColor = [UIColor colorWithRed:0.15 green:0.14 blue:0.16 alpha:1];
    }
    else {
        
        self.tableView.backgroundColor = BACKGRONDCOLOR;
    }
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.tableView registerClass:[CJSHotTableViewCell class] forCellReuseIdentifier:@"hotCell"];
    [self addSubview:self.tableView];
    self.tableView.separatorStyle = NO;
    
    
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self.delegate getDataWithStart:self.start];
        self.start++;
        
    }];
    
    self.tableView.mj_footer = [MJRefreshAutoFooter footerWithRefreshingBlock:^{
        
        [self.delegate getDataWithStart:self.start];
        self.start++;
    }];
    [self.tableView.mj_header beginRefreshing];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.tenArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return 275;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    CJSHotTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"hotCell"];
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    cell.typeTen = [self.tenArray objectAtIndex:indexPath.row];
    cell.contentView.backgroundColor = self.backgroundColor;
    cell.backgroundColor = [UIColor whiteColor];
    CGRect frame = cell.contentView.frame;
    frame.size.height = 275;
    cell.contentView.frame = frame;
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    CJSStoryViewController *storyVC = [[CJSStoryViewController alloc] init];
    
    storyVC.spot_id = [[self.tenArray objectAtIndex:indexPath.row] spot_id];
    [self.delegate pushDetailViewController:storyVC];

}

#pragma - 结束刷新
- (void)endHeaderRefresh {
    
    [self.tableView.mj_header endRefreshing];
    [self.tableView reloadData];
}

- (void)endFooterRefresh {
    
    [self.tableView.mj_footer endRefreshing];
    [self.tableView reloadData];
}



/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
